package cn.geekyspace.scanhive.domain.entity;

import com.baomidou.mybatisplus.annotation.IdType;
import com.baomidou.mybatisplus.annotation.TableId;
import com.baomidou.mybatisplus.annotation.TableName;
import lombok.AllArgsConstructor;
import lombok.Builder;
import lombok.Getter;
import lombok.NoArgsConstructor;
import lombok.Setter;
import lombok.ToString;

import java.time.LocalDateTime;
import java.time.format.DateTimeFormatter;

/**
 * 清洗数据
 */
@Getter
@Setter
@Builder
@ToString
@TableName("cleaning_data")
@NoArgsConstructor
@AllArgsConstructor
public class CleaningData {

    private static final DateTimeFormatter DATE_TIME_FORMATTER = DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss");

    /** 主键 */
    @TableId(type = IdType.AUTO)
    private Integer id;

    /** 条码 */
    private String qrCode;

    /** 清洗时长（秒） */
    private Integer cleaningDuration;

    /** 清洗温度（℃） */
    private Double cleaningTemperature;

    /** 漂洗时长（秒） */
    private Integer rinsingDuration;

    /** 漂洗温度（℃） */
    private Double rinsingTemperature;

    /** 烘干时长（秒） */
    private Integer dryingDuration;

    /** 烘干温度（℃） */
    private Double dryingTemperature;

    /** 条码扫码时间 */
    private LocalDateTime qrCodeScanTime;

    /** PLC数据回传时间 */
    private LocalDateTime plcDataReturnTime;

    /** MES钢网清洗状态（0：默认，200：成功，其他：失败） */
    private Integer gwqxStatus;

    /** 操作员 */
    private String operatorUser;

    public CleaningData(String qrCode, String operatorUser) {
        this.qrCode = qrCode;
        this.operatorUser = operatorUser;
        this.qrCodeScanTime = LocalDateTime.now();
    }

    /** 获取格式化后的二维码扫码时间 */
    public String getFormattedQrCodeScanTime() {
        return qrCodeScanTime != null ? qrCodeScanTime.format(DATE_TIME_FORMATTER) : "";
    }

    /** 获取格式化后的PLC回传时间 */
    public String getFormattedPlcDataReturnTime() {
        return plcDataReturnTime != null ? plcDataReturnTime.format(DATE_TIME_FORMATTER) : "";
    }

    public CleaningData copy() {
        return CleaningData.builder()
                .id(this.id)
                .qrCode(this.qrCode)
                .cleaningDuration(this.cleaningDuration)
                .cleaningTemperature(this.cleaningTemperature)
                .rinsingDuration(this.rinsingDuration)
                .rinsingTemperature(this.rinsingTemperature)
                .dryingDuration(this.dryingDuration)
                .dryingTemperature(this.dryingTemperature)
                .qrCodeScanTime(this.qrCodeScanTime)
                .plcDataReturnTime(this.plcDataReturnTime)
                .gwqxStatus(this.gwqxStatus)
                .operatorUser(this.operatorUser)
                .build();
    }

    public void mergePlcData(CleaningData plcData) {
        if (plcData == null) return;

        this.cleaningDuration = plcData.getCleaningDuration();
        this.cleaningTemperature = plcData.getCleaningTemperature();
        this.rinsingDuration = plcData.getRinsingDuration();
        this.rinsingTemperature = plcData.getRinsingTemperature();
        this.dryingDuration = plcData.getDryingDuration();
        this.dryingTemperature = plcData.getDryingTemperature();
        this.plcDataReturnTime = plcData.getPlcDataReturnTime();
        this.gwqxStatus = plcData.getGwqxStatus();
    }

}



